package com.smooth.site.sx.timer;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smooth.dao.gen.BaseAreasMapper;
import com.smooth.dao.gen.BaseCitiesMapper;
import com.smooth.dao.gen.BaseProvincesMapper;
import com.smooth.dao.gen.bean.BaseAreas;
import com.smooth.dao.gen.bean.BaseAreasExample;
import com.smooth.dao.gen.bean.BaseCities;
import com.smooth.dao.gen.bean.BaseCitiesExample;
import com.smooth.dao.gen.bean.BaseProvinces;
import com.smooth.dao.gen.bean.BaseProvincesExample;

public class AreaDataMain2 {
	static Logger log = LoggerFactory.getLogger(AreaDataMain2.class);

	public static void main(String[] args) {
		try {
			String springFileXml = "classpath*:spring-context*.xml";
			log.info(springFileXml);
			ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(springFileXml);

			BaseAreasMapper baseAreasMapper = applicationContext.getBean(BaseAreasMapper.class);
			BaseCitiesMapper baseCitiesMapper = applicationContext.getBean(BaseCitiesMapper.class);
			BaseProvincesMapper baseProvincesMapper = applicationContext.getBean(BaseProvincesMapper.class);

			List<BaseProvinces> adflist = baseProvincesMapper.selectByExample(new BaseProvincesExample());
			toFile(getProvinceRspF(adflist),"provinces");
			
			
			
			List<BaseAreas> alist = baseAreasMapper.selectByExample(new BaseAreasExample());
			
			List<BaseCities> adlist = baseCitiesMapper.selectByExample(new BaseCitiesExample());
			List<BaseCitiesExtend> belist = getCityRspD(adlist);
			Map<String,List<BaseCitiesExtend>> cmap = getPMap(belist);
			toMapFile(cmap,"city");
			 
			List<AreaExtend> aelist = getRsp(alist);
			Map<String,List<AreaExtend>>  areaMap = getAreaPMap(aelist);
			toMapFile(areaMap,"area");
		} catch (Exception e) {
			log.error("", e);
		}
	}
	

	public static Map<String,List<BaseCitiesExtend>> getPMap(List<BaseCitiesExtend> adlist){
		Map<String,List<BaseCitiesExtend>> bmap = new HashMap<String,List<BaseCitiesExtend>>();
		for(BaseCitiesExtend baseCities : adlist) {
			List<BaseCitiesExtend> blist = bmap.get(baseCities.getProvinceId());
			if(blist==null) {
				blist = new ArrayList<>();
			}
			blist.add(baseCities);
			bmap.put(baseCities.getProvinceId(), blist);
		}
		return bmap;
	}
	
	
	public static Map<String,List<AreaExtend>> getAreaPMap(List<AreaExtend> adlist){
		Map<String,List<AreaExtend>> bmap = new HashMap<String,List<AreaExtend>>();
		for(AreaExtend areaExtend : adlist) {
			List<AreaExtend> blist = bmap.get(areaExtend.getCityId());
			if(blist==null) {
				blist = new ArrayList<>();
			}
			blist.add(areaExtend);
			
			bmap.put(areaExtend.getCityId(), blist);
		}
		return bmap;
	}
	public static void toFile(List<?> aelist,String jname) throws IOException {
		JSONObject jobj = new JSONObject();
		JSONArray jarray = new JSONArray();
		jarray.addAll(aelist);
		jobj.put(jname, jarray);
		log.info(JSON.toJSONString(jobj));
		
		File file = new File("D:\\"+jname+".json");
		FileUtils.write(file, JSON.toJSONString(jobj));
	}
	
	public static void toMapFile(Map bmap,String jname) throws IOException {
		JSONObject jobj = new JSONObject();
		jobj.put(jname, bmap);
		log.info(JSON.toJSONString(jobj));
		
		File file = new File("D:\\"+jname+".json");
		FileUtils.write(file, JSON.toJSONString(jobj));
	}
	public static List<AreaExtend> getRsp(List<BaseAreas> sbList) {

		List<AreaExtend> rlist = new ArrayList<>();
		for (BaseAreas baseAreas : sbList) {
			AreaExtend areaExtend = new AreaExtend();

			try {
				BeanUtils.copyProperties(areaExtend, baseAreas);
			} catch (Exception e) {
				log.error("", e);
			}
			areaExtend.setShowName(baseAreas.getArea());
			rlist.add(areaExtend);
		}
		return rlist;
	}

	public static List<BaseCitiesExtend> getCityRspD(List<BaseCities> sbList) {

		List<BaseCitiesExtend> rlist = new ArrayList<>();
		for (BaseCities baseCities : sbList) {
			BaseCitiesExtend areaExtend = new BaseCitiesExtend();

			try {
				BeanUtils.copyProperties(areaExtend, baseCities);
			} catch (Exception e) {
				log.error("", e);
			}
			areaExtend.setShowName(baseCities.getCityName());
			rlist.add(areaExtend);
		}
		return rlist;
	}

	public static List<BaseProvincesExtend> getProvinceRspF(List<BaseProvinces> sbList) {

		List<BaseProvincesExtend> rlist = new ArrayList<>();
		for (BaseProvinces baseProvinces : sbList) {
			BaseProvincesExtend baseProvincesExtend = new BaseProvincesExtend();

			try {
				BeanUtils.copyProperties(baseProvincesExtend, baseProvinces);
			} catch (Exception e) {
				log.error("", e);
			}
			baseProvincesExtend.setShowName(baseProvinces.getProvinceName());
			rlist.add(baseProvincesExtend);
		}
		return rlist;
	}

	public static class AreaExtend extends BaseAreas {
		private String showName;

		public String getShowName() {
			return showName;
		}

		public void setShowName(String showName) {
			this.showName = showName;
		}
	}

	public static class BaseCitiesExtend extends BaseCities {
		private String showName;

		public String getShowName() {
			return showName;
		}

		public void setShowName(String showName) {
			this.showName = showName;
		}
	}

	public static class BaseProvincesExtend extends BaseProvinces {
		private String showName;

		public String getShowName() {
			return showName;
		}

		public void setShowName(String showName) {
			this.showName = showName;
		}
	}
}
